*** LOADING DATA FILE
cd "C:\Users\au22016\Dept of Pol Science Dropbox\Rune Stubager\02_ClassIDscandi\Data\ANES" // replace with your directory
use "replicationfile_US.dta", clear

*** ANALYSES 
cd "C:\Users\\`=c(username)'\Dept of Pol Science Dropbox\Rune Stubager\02_ClassIDscandi\Outputs\Final figures" // replace with your directory

*** PART 1: INDIVIDUAL-LEVEL ANALYSES ***

* Creating the US part of Figure 1: Distribution of class identity over time
preserve
drop if year<1956
tabulate classid5, generate(var)
collapse (sum) var1 var2 var3 var4 var5, by(year)
gen p1 = var1 / (var1 +var2 +var3 +var4 +var5)
gen p2 = (var1+var2) / (var1 +var2 +var3 +var4 +var5)
gen p3 = (var1+var2+var3) / (var1 +var2 +var3 +var4 +var5)
gen p4 = (var1+var2+var3+var4) / (var1 +var2 +var3 +var4 +var5)
gen p5 = 1
gen zero = 0

twoway rarea zero p1 year, color(gs0) || rarea p1 p4 year, color(gs14) || rarea p4 p5 year, color(gs8) legend(order(1 "Working class ID" 2 "No class ID" 3 "Middle class ID" )) ytitle(Percent) xla(1956 1960 1964 1968 1972 1976 1980 1984 1988 1992 2000 2004 2008 2012 2016 2020, angle(45)) xtitle("") title("United States") // saving(classdist3US, replace)
restore

graph use classdist3US.gph // 
graph export classdist3US.png, replace

* Creating the US part of Figure 2: Class identification and attitudes to redistribution 

eststo redistus: reg jobinc i.classid2##i.year i.agekat i.gender i.edu2 i.income
margins year#classid2
marginsplot, title("United States") xtitle("") ytitle("Redistribution") recastci(rarea) xlabel(, angle(45)) ciopt(color(%20)) // saving(eclr2multiUS.gph, replace)
// graph export eclr2multiUS.png, replace

* Creating the US part of Figure 3: Class identification and attitudes to immigration

eststo immigus: reg immig i.classid2##i.year i.agekat i.gender i.edu2 i.income
margins year#classid2
marginsplot,  title("United States") xtitle("") ytitle("Immigration attitudes") recastci(rarea) xlabel(, angle(45)) ylabel(.4(.1).9) ciopt(color(%20)) // saving(img2multiUS.gph, replace)
// graph export img2multiUS.png, replace

* Creating the US part of Figure 6: Class id and vote choice

eststo partyus: mlogit party2 i.classid2##i.year i.agekat i.gender i.edu2 i.income

margins year#classid2, predict(outcome(1))
marginsplot, title("Democrat") xtitle("") ytitle("% support Dem.") xlabel(, angle(45)) recastci(rarea)legend(row(1)) ciopt(color(%20))  ylab(0(0.1).8) // saving(dem2multiUS.gph, replace) 
// graph export dem2multiUS.png, replace

margins year#classid2, predict(outcome(0))
marginsplot, title("Republican") xtitle("") ytitle("% support Rep.") xlabel(, angle(45)) recastci(rarea) ciopt(color(%20)) ylab(0(0.1).8) // saving(rep2multiUS.gph, replace) 
// graph export rep2multiUS.png, replace

*** PART 2: ANALYSES OF PARTY POLARIZATION INTERACTION ***

merge m:1 year using "C:\Users\au22016\Dept of Pol Science Dropbox\Rune Stubager\02_ClassIDscandi\Data\ANES\USmanifest.dta" // replace with your directory
drop if _merge==1

xtset year

gen ecopos= ecoposus
gen autlib= autlibus3

**Models in main paper, Table 2 (Full model in Table A6.4)

eststo m1us: xtmlogit party2 i.classid2##c.ecopos i.gender i.edu2 i.agekat i.income if party2<3, base(1) re
// estimates save "C:\Users\au22016\Dept of Pol Science Dropbox\Rune Stubager\02_ClassIDscandi\Data\m1us", replace
test 1.classid2#c.ecopos
eststo m2us: xtmlogit party2 i.classid2##c.autlib i.gender i.edu2 i.agekat i.income if party2<3, base(1) re
// estimates save "C:\Users\au22016\Dept of Pol Science Dropbox\Rune Stubager\02_ClassIDscandi\Data\m2us", replace
test 1.classid2#c.autlib
eststo m3us: xtmlogit party2 i.classid2##c.ecopos i.classid2##c.autlib i.gender i.edu2 i.agekat i.income if party2<3, base(1) re
// estimates save "C:\Users\au22016\Dept of Pol Science Dropbox\Rune Stubager\02_ClassIDscandi\Data\m3us", replace
test 1.classid2#c.ecopos
test 1.classid2#c.autlib

reg party2 i.classid2 c.ecopos c.autlib i.gender i.edu2 i.agekat i.income if party2<3, r
estat vif


